#include<bits/stdc++.h>
using namespace std;
int T,n,a[200010],ans=0,logg[32]={1},b[200010];
int flag[1000010],fr[200010],f[200010];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>T;
    for(int i=1;i<=31;i++)
        logg[i]=logg[i-1]*2;
    while(T>0){
        memset(flag,0,sizeof(flag));
        memset(f,0,sizeof(f));
        memset(fr,0,sizeof(fr));
        T--;
        cin>>n;
        if(n<=30){
            for(int i=1;i<=n;i++)
                cin>>a[i];
            int cnt,ma=0;
            for(int i=0;i<=logg[n]-1;i++){
                int ant=i,ans=0;
                cnt=1;
                while(ant>0){
                    b[cnt++]=ant%2;
                    ant/=2;
                }
            /*cout<<i<<" ";
            for(int j=1;j<=n;j++)
                cout<<b[j]<<" ";
            cout<<endl;*/
                for(int j=1;j<=n;j++){
                    for(int k=j+1;k<=n;k++){
                        if(b[j]==b[k]){
                            if(a[j]==a[k])ans+=a[j];
                            break;
                        }
                    }
                }
                ma=max(ma,ans);
            }
            cout<<ma<<endl;
        }
        else{
            ans=0;
            cin>>n;
            for(int i=1;i<=n;i++){
                cin>>a[i];
                if(flag[a[i]]){
                    fr[i]=flag[a[i]];
                    if(fr[i]==i-1){
                        ans+=a[i];
                        fr[i]=-1;
                    }
                }
                flag[a[i]]=i;
            }
            /*if(T==1)
                for(int i=1;i<=n;i++)
                    cout<<fr[i]<<endl;*/
       // if(T==1)cout<<ans<<endl;
            for(int i=1;i<=n;i++){
                if(fr[i]!=-1&&fr[i]!=0){
                    f[i]=f[fr[i]+1]+a[i];
                }
            }
            int ma=0;
            for(int i=1;i<=n;i++){
                //cout<<i<<" "<<f[i]<<" "<<fr[i]<<endl;
                ma=max(ma,f[i]);
            }
            cout<<ma+ans<<endl;
        }
    }
    return 0;
}
